function [dataInRect,dataInd] = getDataInRect( p1, p2, hline )
 
% Define low and high x and y values, rbbox will reverse them if you draw rectangle from bottom up
if ( p1(1) < p2(1) )
   lowX = p1(1); highX = p2(1);
else
   lowX = p2(1); highX = p1(1);
end
 
if ( p1(2) < p2(2) )
   lowY = p1(2); highY = p2(2);
else
   lowY = p2(2); highY = p1(2);
end
 
xdata = get(hline,'XData');
ydata = get(hline,'YData');
 
xind = (xdata >= lowX & xdata <= highX);
yind = (ydata >= lowY & ydata <= highY);
 
dataInd = xind & yind; % these are the indices in xdata and ydata where the points lie within the rectangle
dataInRect = [xdata(dataInd);ydata(dataInd)]'; % this returns all of the data inside the rect in one 2xN matrix
 
end